import cn.hxz.bean.Student;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

    // spring 上下文管理，管理bean的创建，获取
    ApplicationContext ctx = null;

    @Before
    public void setUp() {
        //读取spring配置文件
        ctx = new ClassPathXmlApplicationContext("spring.xml");
    }

    //测试加载bean
    @Test
    public void test1() {
        Student student = (Student)ctx.getBean("student");
        System.out.println(student);
    }
    //有参构造方式
    @Test
    public void test2() {
        Student student = ctx.getBean("studentParam", Student.class);
        System.out.println(student);
    }

    //静态工厂实例
    @Test
    public void test3() {
        Student student = ctx.getBean("studentFactory", Student.class);
        System.out.println(student);
    }

    //singleton测试
    @Test
    public void test4() {
        Student student = ctx.getBean("student", Student.class);
        Student student2 = ctx.getBean("student", Student.class);

        System.out.println(student);
        System.out.println(student2);
        System.out.println(student == student2);
    }
    //多例测试
    @Test
    public void test5() {
        Student student = ctx.getBean("studentProto", Student.class);
        Student student2 = ctx.getBean("studentProto", Student.class);
        System.out.println(student);
        System.out.println(student2);
        System.out.println(student == student2);
    }

    //set方式注入
    @Test
    public void test6() {
        Student student = ctx.getBean("studentSet", Student.class);
        System.out.println(student);
    }
    //p标签注入方式
    @Test
    public void test7() {
        Student student = ctx.getBean("studentP", Student.class);
        System.out.println(student);
    }

    //依赖注入实例
    @Test
    public void test8() {
        Student student = ctx.getBean("studentDI", Student.class);
        System.out.println(student);
    }

    //bean 生命周期
    @Test
    public void test9() {
        Student student = ctx.getBean("studentPeriod", Student.class);
        System.out.println(student);
        ((ClassPathXmlApplicationContext)ctx).close();
        System.out.println(student);
    }

    //p标签对象引用注入方式
    @Test
    public void test10() {
        //Before
        Student student = ctx.getBean("studentPRef", Student.class);

        System.out.println(student);
        return; //AfterReturning
        // AfterThrowing
        //After
        // Around
    }

}
